<?php
/**
 * Created by PhpStorm.
 * User: inhere
 * Date: 2017-02-27
 * Time: 19:19
 */

// add service
$app->set('db', function(\micro\AppInterface $app)
{
    $config = [
        'mode' => \micro\db\SimpleDb::MODE_MASTER_SLAVE,
        'master' => $app->config('db-master'),
        'slaves' => [
            's1' => $app->config('db-slave'),
        ],
    ];

    $db = \micro\db\SimpleDb::createClient($config);

    // add some event
    $db->on('connect', function ($name, $mode, $config) use($app)
    {
        $app->logger->debug("Connect to the $name, mode: $mode", $config);
    });

    $db->on('beforeExecute', function ($sql, $type, $data) use($app)
    {
        $app->logger->debug("TYPE: $type, SQL: $sql", $data);
    });

    return $db;
});

$app->set('taskLog', function(\micro\AppInterface $app)
{
    $options = $app->config('taskLog', []);
    $options['debug'] = $app->isDebug();
    $options['basePath'] = PROJECT_PATH . '/' . $options['basePath'];

    return \inhere\library\utils\LiteLogger::make($options);
});

$app->set('logger', function(\micro\AppInterface $app)
{
    $options = $app->config('log', []);
    $options['debug'] = $app->isDebug();
    $options['basePath'] = PROJECT_PATH . '/' . $options['basePath'];

    return \inhere\library\utils\LiteLogger::make($options);
});
